package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.cache.UserCache;
import com.conversationboard.model.User;
import com.conversationboard.model.useraccess.UserAccessInfo;

@WebServlet(name = "RecentActiveUsersControllerServlet", urlPatterns = "/RecentActiveUsersControllerServlet")
public class RecentActiveUsersControllerServlet extends HttpServlet {

	private static final long serialVersionUID = 6827849282228506105L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			response.setCharacterEncoding("UTF-8");
			request.setCharacterEncoding("UTF-8");

			/* Force an update of the reader's last accessed date */
			User viewingUser = User.get(request.getUserPrincipal().getName());

			List<UserAccessInfo> users = UserCache.getInstance().getRecentlyActiveLoggedInUsers(viewingUser);
			request.setAttribute("users", users);

			/* Site admins can see user info */

			if (viewingUser.isSiteAdmin()) {
				request.setAttribute("siteAdmin", true);
			}

			RequestDispatcher dispatcher = request.getRequestDispatcher("/Pages/recent-active-users.jsp");
			dispatcher.forward(request, response);

		} catch (SQLException e) {
			throw new ServletException(e);
		}

	}

}
